﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server.ActivityFeed;
using ESF.SharePoint.Utility;
using ESF.SharePoint.Entities;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Portal.WebControls;
using System.Globalization;

namespace ESF.SharePoint.Formatters
{
    class CollegueAddictionPropertyFormatter : ActivityFormatter
    {

        public override ActivityEvent Format(ActivityEvent activity, object parameters)
        {
            return null;
        }

        public override CustomActivityEvent Format(CustomActivityEvent activity, object parameters)
        {
            List<Link> colleguesAdded = (List<Link>)parameters;

            if (colleguesAdded != null && colleguesAdded.Count > 0)
            {
                ActivityTemplateVariable atv = activity.ATV;
                atv.Name = string.Format("{0} new collegues", colleguesAdded.Count);

                StringBuilder accounts = new StringBuilder();
                for (int i = 0; i<colleguesAdded.Count; i++)
                {
                    var entity = colleguesAdded[i];
                    //string picture = atv.Publisher.Picture;
                    //picture = ESF.SharePoint.Utility.Utilities.GetUserPicture(picture);
                    string userHtml = string.Format("<a href=\"{0}\" alt=\"{1}\">{1}</a>",
                        SPHttpUtility.HtmlUrlAttributeEncode(SPHttpUtility.UrlPathEncode(entity.Href, false)),
                        entity.Name);
                    accounts.Append(userHtml);

                    if (i == colleguesAdded.Count - 2)
                        accounts.Append(" and ");
                    else if (i < colleguesAdded.Count - 1) 
                        accounts.Append(", ");
                    
                }
                atv.Value = accounts.ToString();

                activity.Modified = true;
            }

            return activity;
        }

    }
}
